package com.atguigu.gmall.product.config;

import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.InputStream;


@Slf4j
@ConfigurationProperties(prefix = "minio")
@Configuration
@Data
public class MinioConfig {

    private String url;
    private String accessKey;
    private String secretKey;
    private String defaultBucket;


    @Bean
    public MinioClient minioClient(){
        try {
            MinioClient minioClient = new MinioClient(
                    url,
                    accessKey,
                    secretKey);
            boolean exists = minioClient.bucketExists(defaultBucket);
            if(!exists){
                minioClient.makeBucket(defaultBucket);
            }
            return minioClient;
        } catch (Exception e) {

            //The difference between the request time and
            // the server's time is too large.
            //服务器里面docker 安装的minio 没有做时间同步
            log.error("minio-client故障：{}",e);
            return null;
        }
    }



}
